#include "cards.h"

Cards::Cards()
{


}

void Cards::addCard(const Card &card)
{
    this->m_cards.insert(card);
}

void Cards::addCards(const Cards &cards)
{
    this->m_cards.unite(cards.m_cards);
}

void Cards::removeCard(const Card &card)
{
    this->m_cards.remove(card);
}

void Cards::removeCards(const Cards &cards)
{
    this->m_cards.subtract(cards.m_cards);
}

void Cards::clear()
{
    this->m_cards.clear();
}

int Cards::cardCount(Card &card)
{
    return this->m_cards.size();
}

bool Cards::IsEmpty()
{
    return this->m_cards.isEmpty();
}

CardPoint Cards::getMaxPoint()
{
    CardPoint max = CardPoint::C_Begin;
    if(!m_cards.isEmpty())
    {
        for(auto it = m_cards.begin();it!=m_cards.end();it++)
        {
             Card card = *it;
             if(card.getPoint()>max)
             {
                 max = card.getPoint();
             }
        }
    }
    return max;
}

CardPoint Cards::getMinPoint()
{

    CardPoint min = CardPoint::C_End;
    if(!m_cards.isEmpty())
    {
        for(auto it = m_cards.begin();it!=m_cards.end();it++)
        {
             Card card = *it;
             if(card.getPoint()<min)
             {
                 min = card.getPoint();
             }
        }
    }
    return min;
}

Card Cards::getRandomCard()
{
    //先生成一个随机数
    int num = QRandomGenerator::global()->bounded(m_cards.size());
    auto it = m_cards.begin();
    for(int i = 0;i<num;i++) it++;
    Card card = *it;
    m_cards.erase(it);
    return card;
}

QVector<Card> Cards::cardSort(SortType type)
{
    //先转换成vector
    QVector<Card>list;
    for(auto it = m_cards.begin();it!=m_cards.end();it++)
    {
        list<<*it;
    }

    if(type == SortType::Asc)
    {
       //使用std里面的sort进行排序
        std::sort(list.begin(),list.end(),[=](Card card1,Card card2){
           if(card1.getPoint()>card2.getPoint())
           {
                  return true;
           }
           else
           {
               if(card1.getSuit()>card2.getSuit())
               {
                   return true;
               }
           }

           return false;
        });
    }
    else if(type == SortType::Desc)
    {
        //使用std里面的sort进行排序
         std::sort(list.begin(),list.end(),[=](Card card1,Card card2){
            if(card1.getPoint()<card2.getPoint())
            {
                   return true;
            }
            else
            {
                if(card1.getSuit()<card2.getSuit())
                {
                    return true;
                }
            }

            return false;
         });


    }

    return list;
}




